/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package services;

import commons.Constants;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Leonardo Oliveira Moreira
 */
public class VMListService extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/xml;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String vmId = request.getParameter("vmId");
        StringBuilder xml = new StringBuilder("");
        try {
            Class.forName(Constants.DB_DRIVER);
            Connection connection = DriverManager.getConnection(Constants.DB_ADDRESS, Constants.DB_USERNAME, Constants.DB_PASSWORD);
            Statement statement = connection.createStatement();
            ResultSet resultSet = null;
            if (vmId == null || vmId.trim().length() == 0) {
                resultSet = statement.executeQuery("select id, name, address, active, cpu_free, mem_total, mem_free, disk_total, disk_free from vm");
                xml.append("<vm_list>");
                while (resultSet.next()) {
                    xml.append("<vm>");
                    xml.append("<id>" + resultSet.getInt("id") + "</id>");
                    xml.append("<name>" + resultSet.getString("name") + "</name>");
                    xml.append("<address>" + resultSet.getString("address") + "</address>");
                    xml.append("<active>" + resultSet.getBoolean("active") + "</active>");
                    xml.append("<cpu_free>" + resultSet.getLong("cpu_free") + "</cpu_free>");
                    xml.append("<mem_total>" + resultSet.getLong("mem_total") + "</mem_total>");
                    xml.append("<mem_free>" + resultSet.getLong("mem_free") + "</mem_free>");
                    xml.append("<disk_total>" + resultSet.getLong("disk_total") + "</disk_total>");
                    xml.append("<disk_free>" + resultSet.getLong("disk_free") + "</disk_free>");
                    xml.append("</vm>");
                }
                xml.append("</vm_list>");

            } else {
                resultSet = statement.executeQuery("select id, name, address, active, cpu_free, mem_total, mem_free, disk_total, disk_free from vm where id = " + vmId);
                while (resultSet.next()) {
                    xml.append("<vm>");
                    xml.append("<id>" + resultSet.getInt("id") + "</id>");
                    xml.append("<name>" + resultSet.getString("name") + "</name>");
                    xml.append("<address>" + resultSet.getString("address") + "</address>");
                    xml.append("<active>" + resultSet.getBoolean("active") + "</active>");
                    xml.append("<cpu_free>" + resultSet.getLong("cpu_free") + "</cpu_free>");
                    xml.append("<mem_total>" + resultSet.getLong("mem_total") + "</mem_total>");
                    xml.append("<mem_free>" + resultSet.getLong("mem_free") + "</mem_free>");
                    xml.append("<disk_total>" + resultSet.getLong("disk_total") + "</disk_total>");
                    xml.append("<disk_free>" + resultSet.getLong("disk_free") + "</disk_free>");
                    xml.append("</vm>");
                }
            }
            resultSet.close();
            statement.close();
            connection.close();
        } catch (ClassNotFoundException ex) {
            xml = new StringBuilder("");
            xml.append("<error>");
            xml.append("<message>" + ex.getMessage() + "</message>");
            xml.append("</error>");
        } catch (SQLException ex) {
            xml = new StringBuilder("");
            xml.append("<error>");
            xml.append("<message>" + ex.getMessage() + "</message>");
            xml.append("</error>");
        }
        try {
            out.print(xml.toString());
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
